﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using Mimoza.Common;

namespace Mimoza.GenerateSystemUpdate
{
    class Program
    {
        static void Main(string[] args)
        {

            if (args.Length != 6)
            {
                Console.WriteLine("The syntax of the command is incorrect.");
                Console.WriteLine("   Use the following syntax 'GenerateSystemUpdate %updateName %OSName %ModelName %Version %sourceFILE %destinationFILE'");

                return;
            }

            using (FileStream source = new FileStream(args[4], FileMode.Open, FileAccess.Read))
            using (FileStream dist = new FileStream(args[5], FileMode.Create, FileAccess.Write))
            {
                UpdatesFileInfo info = new UpdatesFileInfo();
                info.Name = args[0];
                info.OSName = args[1];
                info.ModelName = args[2];
                info.Version = args[3];
                info.Size = source.Length;

                switch (info.ModelName)
                {
                    case Mimoza.Common.Model.C36SX_DisplayName:
                    case Mimoza.Common.Model.L66_DisplayName:
                    case Mimoza.Common.Model.LX_DisplayName:
                    case Mimoza.Common.Model.VPC_DisplayName: 
                        break;
                    default:
                        throw new Exception("Unknown platform - " + info.ModelName);                            
                }
                

                info.WriteInfo(dist);

                int readed = 0;
                int bufferSize = 2048 * 100;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = source.Read(buffer, 0, bufferSize);
                readed += readCount;
                while (readCount > 0)
                {
                    dist.Write(buffer, 0, readCount);
                    readCount = source.Read(buffer, 0, bufferSize);
                    readed += readCount;
                }
            }
        }
    }
}

